#!/usr/bin/python
# encoding: utf-8
import re
import json
import requests
import time
import sys
 
reload(sys)
sys.setdefaultencoding('utf-8')
# 构造20页所有的url
urls = []
url_format = "https://s.taobao.com/search?q=%E6%89%8B%E6%9C%BA&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.50862.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170803&p4ppushleft=5%2C48&s={}"
for i in range(0, 20):
    urls.append(url_format.format(i*48))


# 抓取每页的信息
for url in urls:
    res = requests.get(url)

    obj_info = re.search(r"g_page_config = ({.*?});", res.text)

    # print(obj_info)

    # print(obj_info==None)

    # print(obj_info)
    if obj_info==None:
        print(obj_info=="None")
    else:
        info = json.loads(obj_info.group(1))

        goods_info = info['mods']['grid']['data']['spus']
        
        for i in goods_info:
            print i['title'], i['price'], i['pic_url'], i["month_sales"]
            # print(i['title'])
    
    # time.sleep(2)
    

    # for i in goods_info:
    #     print i['title'], i['price'], i['pic_url'], i["month_sales"]

